// ==UserScript==
// @name         Infor WMS 后台字段宽度修改
// @version      0.2
// @description  用于修改wms后台页面字段宽度!
// @author       txlkbin
// @updateURL    https://gitee.com/txlkb/wms/raw/master/index.js
// @downloadURL  https://gitee.com/txlkb/wms/raw/master/index.js
// @match        http://*/scprd/sce/sceapp.ctrl?S|G|loginRequest=*
// @icon         https://www.infor.cn/img/logo-infor.png
// ==/UserScript==

(function() {
    this.copyTextToClipboard = function(text) {
        // 创建一个临时的textarea元素
        var textArea = document.createElement("textarea");

        // 设置文本框的值为你要复制的文本
        textArea.value = text;

        // 防止页面滚动
        textArea.style.position = "absolute";
        textArea.style.opacity = "0";

        // 将textarea元素添加到页面中
        document.body.appendChild(textArea);

        // 选择textarea中的文本
        textArea.select();
        textArea.setSelectionRange(0, textArea.value.length); // For mobile devices

        // 执行复制操作
        try {
            var successful = document.execCommand('copy');
            if (successful) {
                console.log('Text successfully copied to clipboard!');
            } else {
                console.error('Unable to copy text to clipboard');
            }
        } catch (err) {
            console.error('Failed to copy text: ', err);
        }

        // 移除临时的textarea元素
        document.body.removeChild(textArea);
    }
    const $ = this.$;
    let btn = document.createElement('div');
    btn.id='zy_update_width_title';
    btn.style.cssText='right: -83px;transition:width 0.3s;-webkit-transition:width 0.3s;width:86px;bottom: 30px;position: fixed;z-index: 99999;border-radius: 10px;background-color: aquamarine;padding: 2px 10px;color: red;font-weight: bold;cursor: pointer;user-select: none;';
    let img = document.createElement('img');
    img.src='images/navigation/labor.svg';
    img.style.width='10px';
    img.style.height='10px';
    btn.insertBefore(img,btn[0]);
    btn.innerHTML = btn.innerHTML+' 修改字段宽度';
    btn.addEventListener('click', function (e) {
        zy_update_show();
        e.preventDefault();
    });
    $(btn).hover(function(){
        $(btn).width("160px");
    },function(){
        $(btn).width("86px");
    });
    document.body.appendChild(btn);
    function addContentBeforeTable(element, content) {
        // 不断向上查找父元素，直到找到第一个table元素
        while (element !== null) {
            if (element.tagName.toLowerCase() === 'table') {
                console.log(element)
                // 找到table元素后，在其前面插入新的内容
                const newElement = document.createElement('div'); // 或其他适当的元素
                newElement.innerHTML = content;
                element.parentNode.insertBefore(newElement, element);
                break;
            }
            element = element.parentElement;
        }
    }
    let isShow = false;
    function zy_update_show(){
        let list = $('table[id$=_table]').children('tbody:first').find('tr.listheader').children('td');
        $('.zyForm').remove();
        $('.epnymenuidList').remove();
        $('.fieldCodeList').remove();
        $('[efid]').html('');
        if(isShow){
            list.find('img[title=zy_img]').remove();
        }else{
            $('[epnymenuid]').each(function() {
                var value = $(this).attr('epnymenuid');
                $(this).after(`<span class="epnymenuidList" style="color: red;cursor: pointer;" onclick="copyTextToClipboard('${value}')" >菜单ID</span>`);
            });
            $('[epnyformwidgetid]').each(function() {
                var value = $(this).attr('epnyformwidgetid');
                $(this).after(`<span class="epnymenuidList" style="color: red;cursor: pointer;" onclick="copyTextToClipboard('${value}')" >组件ID</span>`);
            });
            var FORM_ID = $('#SectionContent0_0').children().first().children().first().attr('efid');
            if(FORM_ID){
                addContentBeforeTable($('table[id$=_table]')[0].parentElement,`<div class="zyForm"><span style="color: red;cursor: pointer;" onclick="copyTextToClipboard('${FORM_ID}')">${FORM_ID}<span></div>`)
            }
            for (let i = 0; i < list.length; i++) {
                let img2 = document.createElement('img');
                img2.title='zy_img';
                img2.src='images/navigation/labor.svg';
                img2.style.width='15px';
                img2.style.height='15px';
                img2.style.cursor='pointer';
                img2.addEventListener('click', function (e) {
                    zy_update(i);
                    e.preventDefault();
                });
                list.eq(i)[0].appendChild(img2);
            }
            $('table[id$=_table]').children('tbody:first').find('tr.listfilter').children('td').each(function(index) {
                var value = $($(this).children('span')[0]).attr('epnybioattributename') || $($(this).children('span')[0]).attr('epnyformwidgetname');
                if(value){
                    $($('table[id$=_table]').children('tbody:first').find('tr.listheader').children('td')[index]).append(`<br /> <span class="fieldCodeList" style="color: red;cursor: pointer;" onclick="copyTextToClipboard('${value}')" >${value}</span>`);
                }
            });
            $('[efid]').each(function(index) {
                $(this).html(`<div calss="zyForm"><span style="color: red;cursor: pointer;" onclick="copyTextToClipboard('${$(this).attr('efid')}')">表单ID<span></div>`);
                $(this).attr('style','display: inline-block;position: inherit;width: inherit;height: inherit;');
            });
        }
        isShow=!isShow
    }
    function zy_update(i){
        const dom = $('table[id$=_table]').children('colgroup:first').find('col').eq(i);
        const width=dom.width();
        const zy_width = prompt('请输入需要设置的宽度（单位：像素）:',width);
        if(!zy_width){
            return;
        }
        dom.width(zy_width);
        const newWidth=dom.width();
        console.log('原宽度：'+width);
        console.log('现宽度：'+newWidth);
        if($('#allstuff').children().eq(3).find('table.epnyInteractionToolbar:first').find('div.epnyFWError').text()===''){
            const toplength = $('#allstuff').children().eq(3).find('table:first').children('tbody:first').children('tr').eq(1).find('div:first');
            let height = toplength.find('div').eq(1);
            height.height(height.height()-16);
            toplength.css('top',toplength.position().top+16);
            height = $('#allstuff').children().eq(3).find('table:first').find('td:first').children('div:first');
            height.height(height.height()+16);
        }
        $('#allstuff').children().eq(3).find('table.epnyInteractionToolbar:first').find('div.epnyFWError').text('点击字段分割线即可对齐并保存！'+'原宽度：'+width+'px,现宽度：'+newWidth+'px');
    }
})();